<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 绑定园所
 */
class Bind extends CT_Controller
{

    public function __construct()
    {
        parent::__construct();
        
        $this->load->model('area_model');
        $this->load->model('city_model');
        $this->load->model('nursery_model');
    }

    /**
     * 地区页
     */
    public function area()
    { 
        //地区信息
        $conditions = array('status'=>1);
        $fields = '*';
        $order_by = 'weight desc';
        $area_info = $this->area_model->findAll($conditions,$fields,$order_by);
        
        $data['area_info'] = $area_info;
        
        $this->load->view('bind/area', $data);
    }
    
    /**
     * 市区页
     */
    public function city()
    {
        $area_id = intval($this->input->get('area_id',TRUE));
        $area_title = $this->input->get('area_title',TRUE);
        
        //地区信息
        $data['area_title'] = $area_title;
        $data['area_id'] = $area_id;
        
        //市区信息
        $conditions = array('area_id'=>$area_id,'status'=>1);
        $fields = '*';
        $order_by = 'weight desc';
        $city_info = $this->city_model->findAll($conditions,$fields,$order_by);
        $data['city_info'] = $city_info;
        
        //获取绑定过的市区信息
        $data['city_id'] = 0;
        if($this->_nursery_id){
            $conditions = array('id'=>$this->_nursery_id, 'status'=>1);
            $nursery_info = $this->nursery_model->find($conditions);
            $data['city_id'] = $nursery_info['city_id'];
        }
    
        $this->load->view('bind/city', $data);
    }
    
    /**
     * 园所页
     */
    public function nursery()
    {
        $area_id = intval($this->input->get('aid',TRUE));
        $area_title = $this->input->get('at',TRUE);
        $city_id = intval($this->input->get('cid',TRUE));
        $city_title = $this->input->get('ct',TRUE);
    
        //地区信息
        $data['area_title'] = $area_title;
        $data['city_title'] = $city_title;
    
        //市区信息
        if($city_id){
            $conditions = array('area_id'=>$area_id,'city_id'=>$city_id,'status'=>1);
        }else{
            $conditions = array('area_id'=>$area_id,'status'=>1);
        }
        $fields = '*';
        $order_by = 'weight desc';
        $nursery_info = $this->nursery_model->findAll($conditions,$fields,$order_by);
        $data['nursery_info'] = $nursery_info;
    
        $this->load->view('bind/nursery', $data);
    }
    
    /**
     * 绑定园所
     */
    public function change()
    {
        $this->load->model('member_model');
        $this->load->model('teacher_model');
        $this->load->library('CT_auth_member_teacher');
        
        $nursery_id = intval($this->input->post('nid',TRUE));
    
        $teacher_info = $this->teacher_model->find(array('wx_open_id'=>$this->_open_id, 'status'=>1));
        $member_info = $this->member_model->find(array('wx_open_id'=>$this->_open_id, 'status'=>1));
        
        $data = array(
            'nursery_id'=>$nursery_id,
            'update_date'=>date('Y-m-d H:i:s')
        );
        
        if ($teacher_info) {
            $this->teacher_model->update($data,array('id'=>$teacher_info['id']));
        }elseif($member_info){
            $this->member_model->update($data,array('id'=>$member_info['id']));
        }
        
        $sess_data = array('nursery_id'=>$nursery_id);
        $this->ct_auth_member_teacher->set_user_login($sess_data);
        
    }

}
